#include<head.h>

int Size(FILE *fp) //计算文件大小
{
    fp = fopen("./1.txt", "r");
    FOPEN_ERR(fp);
    fseek(fp, 0, SEEK_END);
    int size = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    return size;
}

int copy_up(FILE *fp, FILE *fp1 ,int size ) //拷贝文件
{
    fp = fopen("./1.txt", "r");
    FOPEN_ERR(fp);
    char buff[128];
    fread(buff, size/2, 1, fp);//读出内容
    fclose(fp);
    fp1 = fopen("./3.txt", "a+");
    FOPEN_ERR(fp1);
    fwrite(buff, size/2, 1, fp1);//将内容写进目标文件
    fclose(fp1);
}

int copy_down(FILE *fp, FILE *fp1 ,int *size ) //拷贝文件
{
    fp = fopen("./1.txt", "r");
    FOPEN_ERR(fp);
    fseek(fp, *size/2, SEEK_SET);
    char buff[128];
    fread(buff, *size/2, 1, fp);//读出内容
    fclose(fp);
    fp1 = fopen("./3.txt", "a+");
    FOPEN_ERR(fp1);
    fwrite(buff, *size/2, 1, fp1);//将内容写进目标文件
    fclose(fp1);
}

void *task(void *arg)
{
    FILE *fp,*fp1;
    printf("子线程id\n");
    int *size = (int*)arg;
    sleep(2);
    copy_down(fp, fp1, size);
}

int main(int argc, char const *argv[])
{
    FILE *fp,*fp1;
    int size = Size(fp);
    pthread_t tid;
    if(pthread_create(&tid, NULL, task, &size) != 0)
    {
        perror("pthread_create");
        return -1;
    }
    printf("主线程id\n");
    copy_up(fp, fp1, size);
    while(1);
    return 0;
}
